package com.bsb.biz.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bsb.base.mapper.SysRoleDeptMapper;
import com.bsb.base.model.SysRoleDept;
import com.bsb.base.model.SysRoleDeptExample;
import com.bsb.base.model.SysRoleDeptExample.Criteria;

@Service
public class SysRoleDeptService{
	
	@Resource
	private SysRoleDeptMapper sysRoleDeptMapper;
	
	public void addSysRoleDept(SysRoleDept sysRoleDept) {
		sysRoleDeptMapper.insertSelective(sysRoleDept);
	}
	
	public void delSysRoleDeptById(Long id) {
		sysRoleDeptMapper.deleteByPrimaryKey(id);
	}
	
	public void updateSysRoleDept(SysRoleDept sysRoleDept) {
		sysRoleDeptMapper.updateByPrimaryKeySelective(sysRoleDept);
	}
	
	public void updateSysRoleDeptByExample(SysRoleDept sysRoleDept,SysRoleDeptExample sysRoleDeptExample){
	    sysRoleDeptMapper.updateByExampleSelective(sysRoleDept,sysRoleDeptExample);
	}
	
	public SysRoleDept getSysRoleDeptById(Long id) {
		return sysRoleDeptMapper.selectByPrimaryKey(id);
	}
	
	public List<SysRoleDept> getPageSysRoleDept(SysRoleDeptExample sysRoleDeptExample) {
		return sysRoleDeptMapper.selectPage(sysRoleDeptExample);
	}
	
	public int countSysRoleDept(SysRoleDeptExample sysRoleDeptExample) {
		return sysRoleDeptMapper.countByExample(sysRoleDeptExample);
	}
	
	public List<SysRoleDept> selectByExample(SysRoleDeptExample example){
	    return sysRoleDeptMapper.selectByExample(example);
	}

	public List<Long> queryDeptIdList(Long[] roleIds) {
		return sysRoleDeptMapper.queryDeptIdList(roleIds);
	}
	
	public void insertBatchRoleDept(Long id, List<String> deptlist) {
		for (String deptid : deptlist) {
			SysRoleDept roledept = new SysRoleDept();
			roledept.setDept_id(Long.valueOf(deptid));
			roledept.setRole_id(id);
			sysRoleDeptMapper.insertSelective(roledept);
		}
	}

	public void delByExample(SysRoleDeptExample example) {
		sysRoleDeptMapper.deleteByExample(example);
	}
	
	public List<Long> getDeptListByRoleId(Long id){
		List<Long> deptList = new ArrayList<Long>();
		SysRoleDeptExample example = new SysRoleDeptExample();
		Criteria criteria = example.createCriteria();
		criteria.andRole_idEqualTo(id);
		List<SysRoleDept> list = sysRoleDeptMapper.selectByExample(example );
		for (SysRoleDept sysRoleDept : list) {
			Long dept_id = sysRoleDept.getDept_id();
			deptList.add(dept_id);
		}
		return deptList;
	}
	
	
}
